﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace F2S.LINQ2LinkedIn.Client.Selectors
{
    public class MainPageDataTemplateSelector : DataTemplateSelector
    {
        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {
            if (item is string)
            {
                var dt = (container as FrameworkElement).FindResource("notLoggedInTemplate") as DataTemplate;
                return dt;
            }
            if (item is int)
            {
                var dt = (container as FrameworkElement).FindResource("loggedInTemplate") as DataTemplate;
                return dt;
            }
            return null;
        }
    }

    public static class FrameworkElementExtensions
    {
        public static object FindResource(this FrameworkElement element, object key)
        {
            if (element.Resources.ContainsKey(key))
            {
                return element.Resources[key];
            }
            else
            {
                if (element.Parent != null && element.Parent is FrameworkElement)
                {
                    return ((FrameworkElement) element.Parent).FindResource(key);
                }
                else
                {
                    if (Application.Current.Resources.ContainsKey(key))
                    {
                        return Application.Current.Resources[key];
                    }
                }
            }
            return null;
        }
    }

}
